#!/usr/bin/env bash
#
# Copyright 2016-2018 Axel Huebl, Rene Widera
#
# This file is part of PIConGPU.
#
# PIConGPU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PIConGPU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PIConGPU.
# If not, see <http://www.gnu.org/licenses/>.
#

# search recursive inside the `$1` folder if a file is using the
# wrong include brackets "..." instead of <...> to include external library files
#
# @param $1 path to the folder
# @param $2 prefix of the extern library e.g pmacc, boost
# @result 0 if no files are found, else 1

folderName=$1
libPrefix=$2

ok=0
files=()

pattern="\.def$|\.h$|\.cpp$|\.cu$|\.hpp$|\.tpp$|\.kernel$|\.loader$|"\
"\.param$|\.unitless$"

for i in $(find $folderName \
    -not -path "./.git/*" \
    -type f | \
    grep -P "${pattern}")
do
    grep -q "include.*\"$libPrefix\/.*\"" $i
    if [ $? -eq 0 ]
    then
        files+=($i)
        echo "$i uses \"...\" instead of <...> for '$libPrefix' includes!"
        ok=1
    fi
done

if [ $ok -ne 0 ]
then
    echo ""
    echo "SUMMARY"
    echo "-------"
    echo "Run the following command(s) on the above files to fix"
    echo "the include brackets which are including '$libPrefix' files:"
    echo ""
    for i in ${files[@]}
    do
        echo "sed -i 's/\(include.*\)\"\($libPrefix\/.*\)\"/\1<\2>/' $i"
    done
fi

exit $ok
